package com.longma.server.controller;


import com.longma.server.entity.RealtimeData;
import com.longma.server.pojo.UserResponseEntity;
import com.longma.server.pojo.vo.home.PointVO;
import com.longma.server.service.IRealtimeDataService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */

@RestController
@Validated
@RequestMapping("/api/data")
public class RealtimeDataController {

    @Autowired
    private IRealtimeDataService dataService;

    @ApiOperation(value = "上报数据")
    @GetMapping("/receive")
    public UserResponseEntity<RealtimeData> receiveDataFromYc(@NotBlank(message = "message不能为空") @RequestParam("message") String message)  {
        return UserResponseEntity.success(dataService.receiveDataFromYc(message));
    }

    @ApiOperation(value = "获取实时发电功率曲线")
    @GetMapping("/generation/curve")
    public UserResponseEntity<Map<Integer, List<PointVO>>> getPowerGenerationCurve(@Valid @RequestParam("yuncangIds") List<@NotNull(message = "云仓id不能为空") Integer> yuncangIds){
        return UserResponseEntity.success(dataService.getPowerGenerationCurve(yuncangIds,30));
    }

    @ApiOperation(value = "获取实时用电功率曲线")
    @GetMapping("/consumption/curve")
    public UserResponseEntity<Map<Integer, List<PointVO>>> getPowerConsumptionCurve(@Valid @RequestParam("yuncangIds") List<@NotNull(message = "云仓id不能为空") Integer> yuncangIds){
        return UserResponseEntity.success(dataService.getPowerConsumptionCurve(yuncangIds,30));
    }

    @ApiOperation(value = "获取若干个云仓某个属性的实时曲线")
    @GetMapping("/curve")
    public UserResponseEntity<Map<Integer, List<PointVO>>> getCurve(@Valid @RequestParam("yuncangIds") List<@NotNull(message = "云仓id不能为空") Integer> yuncangIds,
                                                                   @ApiParam("需要绘制曲线的属性名，支持current、voltage、temperature、humidity等") @NotBlank(message = "columnName不能为空") @RequestParam("columnName") String columnName){
        return UserResponseEntity.success(dataService.getCurve(yuncangIds,columnName,30));
    }

    /**
     * 获取实时xxx列表曲线 统计间隔为x分钟  x分钟内如果有多条数据取平均 如果某个时间点没有数据使用插值算法填充
     */
    @ApiOperation(value = "获取单个云仓若干个属性的实时曲线")
    @GetMapping("/curve/list")
    public UserResponseEntity<Map<String, List<PointVO>>> getCurve(@NotNull(message = "yuncangId不能为空") @RequestParam("yuncangId")  Integer yuncangId,
                                               @ApiParam("需要绘制曲线的属性名，支持current、voltage、temperature、humidity等") @Size(min = 1,message = "columnNames不能为空")  @RequestParam("columnNames") List<@NotBlank(message = "columnName不能为空") String> columnNames){
        return UserResponseEntity.success(dataService.getCurve(yuncangId,columnNames,30));
    }

    @ApiOperation(value = "获取实时数据")
    @GetMapping("/get")
    public UserResponseEntity<Map<Integer,RealtimeData>> getLatestRealtimeDataRecord(@Valid @RequestParam("yuncangIds") List<@NotNull(message = "云仓id不能为空") Integer>  yuncangIds){
        return UserResponseEntity.success(dataService.getLatestRealtimeDataRecord(yuncangIds));
    }
}

